<?php
require_once __DIR__ . '/file_storage.php';

// 匿名化IP地址的函数
function anonymize_ip($ip) {
    if (empty($ip)) return '';
    
    // 处理IPv4地址
    if (strpos($ip, '.') !== false) {
        $parts = explode('.', $ip);
        if (count($parts) >= 3) {
            $parts[3] = 'x'; // 隐藏最后一段
            return implode('.', $parts);
        }
    }
    
    // 处理IPv6地址
    if (strpos($ip, ':') !== false) {
        $parts = explode(':', $ip);
        if (count($parts) >= 4) {
            $parts[3] = 'xxxx'; // 隐藏最后一段
            return implode(':', $parts);
        }
    }
    
    return $ip;
}

// 获取真实IP并匿名化
require_once __DIR__ . '/file_storage.php';
$real_ip = get_real_ip();
$ip_anonymized = anonymize_ip($real_ip);

// 记录访问
$log_entry = [
    'ip_anonymized' => $ip_anonymized,
    'ua_hash' => hash('sha256', $_SERVER['HTTP_USER_AGENT'] ?? ''),
    'ua_raw' => false,
    'ref_hash' => hash('sha256', $_SERVER['HTTP_REFERER'] ?? ''),
    'path' => $_SERVER['REQUEST_URI'],
    'method' => $_SERVER['REQUEST_METHOD'],
    'lang' => $_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? '',
    'consent' => false, // 被动追踪默认为不同意
    'gps_lat' => null,
    'gps_lng' => null,
    'gps_acc' => null
];
save_log($log_entry);

// 输出1x1透明GIF
header('Content-Type: image/gif');
echo base64_decode('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7');
?>